/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cadastro;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import utilitario.prd_fornecedor;

/**
 *
 * @author marcos
 */
public class cad_prod_forn extends javax.swing.JFrame {

    /**
     * Creates new form cad_prod_forn
     */
      private ArrayList prod_forn;

      String nome_forn;
      Object nome_forns;

    public cad_prod_forn(String nome_forn) {
        this.nome_forn = nome_forn;
        //pegandos os fornecedores do formulario fornecedor





             initComponents();

        prod_forn = new ArrayList();




         jTable1.getColumnModel().getColumn(0).setPreferredWidth(100);
         jTable1.getColumnModel().getColumn(1).setPreferredWidth(100);
         jTable1.getColumnModel().getColumn(2).setPreferredWidth(50);
         jTable1.getColumnModel().getColumn(3).setPreferredWidth(50);


         carrega_arquivo();



       tf_fornecedor.setText(this.nome_forn);
    }

    public cad_prod_forn() {
        initComponents();


        prod_forn = new ArrayList();


         jTable1.getColumnModel().getColumn(0).setPreferredWidth(100);
         jTable1.getColumnModel().getColumn(1).setPreferredWidth(100);
         jTable1.getColumnModel().getColumn(2).setPreferredWidth(50);
         jTable1.getColumnModel().getColumn(3).setPreferredWidth(60);

         carrega_arquivo();

       tf_fornecedor.setText(this.nome_forn);


    }


    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        tf_nome_prd = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jSeparator1 = new javax.swing.JSeparator();
        tf_prd_valor = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        tf_fornecedor = new javax.swing.JTextField();
        btn_novo_prd = new javax.swing.JButton();
        btn_excluir_prd = new javax.swing.JButton();
        btn_editar_prd = new javax.swing.JButton();
        btn_add = new javax.swing.JButton();
        btn_salvar = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        tf_tempo = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        tf_nome_prd.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tf_nome_prdKeyPressed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("Nome:");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Fornecedor", "Nome", "Preço", "Temp/Ent"
            }
        ));
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jTable1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTable1KeyTyped(evt);
            }
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTable1KeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTable1KeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        tf_prd_valor.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tf_prd_valorKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tf_prd_valorKeyReleased(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setText("Valor:");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel4.setText("Fornecedor:");

        tf_fornecedor.setEditable(false);
        tf_fornecedor.setBackground(new java.awt.Color(204, 204, 204));
        tf_fornecedor.setForeground(new java.awt.Color(204, 0, 0));
        tf_fornecedor.setToolTipText("");
        tf_fornecedor.setFocusable(false);
        tf_fornecedor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tf_fornecedorActionPerformed(evt);
            }
        });

        btn_novo_prd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/novos.png"))); // NOI18N
        btn_novo_prd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_novo_prdActionPerformed(evt);
            }
        });

        btn_excluir_prd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/excluir.png"))); // NOI18N

        btn_editar_prd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/editar.png"))); // NOI18N

        btn_add.setIcon(new javax.swing.ImageIcon(getClass().getResource("/check-64.png"))); // NOI18N
        btn_add.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_addActionPerformed(evt);
            }
        });
        btn_add.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                btn_addKeyPressed(evt);
            }
        });

        btn_salvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/save.png"))); // NOI18N
        btn_salvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_salvarActionPerformed(evt);
            }
        });
        btn_salvar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                btn_salvarKeyPressed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel5.setText("Tempo de Entrega:");

        tf_tempo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tf_tempoKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tf_prd_valor, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3)
                            .addComponent(jLabel1)
                            .addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(tf_tempo)))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(tf_fornecedor, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
                        .addComponent(tf_nome_prd, javax.swing.GroupLayout.Alignment.LEADING)))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(btn_excluir_prd, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btn_editar_prd, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btn_salvar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(0, 111, Short.MAX_VALUE)
                .addComponent(btn_add, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btn_novo_prd, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(407, 407, 407))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(5, 5, 5)
                        .addComponent(tf_fornecedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tf_nome_prd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel5))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(tf_prd_valor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(tf_tempo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btn_add))
                            .addComponent(btn_novo_prd)))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, Short.MAX_VALUE)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(btn_editar_prd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btn_excluir_prd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btn_salvar))
                .addContainerGap(56, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel2.setFont(new java.awt.Font("Cooper Black", 0, 24)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(0, 204, 51));
        jLabel2.setText("Cadastro Produto ");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(190, 190, 190)
                .addComponent(jLabel2)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(144, 144, 144))
        );

        setSize(new java.awt.Dimension(636, 380));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void tf_fornecedorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tf_fornecedorActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tf_fornecedorActionPerformed

    private void btn_addActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_addActionPerformed
        // TODO add your handling code here:
       DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel();

       dtm.addRow(new Object[]{tf_fornecedor.getText(), tf_nome_prd.getText(),tf_prd_valor.getText(),tf_tempo.getText()});

       prod_forn.add(new prd_fornecedor(tf_fornecedor.getText(), tf_nome_prd.getText(),tf_prd_valor.getText(),tf_tempo.getText()));

    }//GEN-LAST:event_btn_addActionPerformed

    private void btn_novo_prdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_novo_prdActionPerformed
        // TODO add your handling code here:
       tf_nome_prd.setText("");
       tf_prd_valor.setText("");
       tf_tempo.setText("");
    }//GEN-LAST:event_btn_novo_prdActionPerformed

    private void btn_salvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_salvarActionPerformed
        // TODO add your handling code here:

        if(tf_nome_prd.getText().equals("")){

            JOptionPane.showMessageDialog(null,"E necessario digitar no campo");
        }
        if(tf_prd_valor.getText().equals("")){

            JOptionPane.showMessageDialog(null,"E necessario digitar no campo");
        }
        if(tf_tempo.getText().equals("")){

            JOptionPane.showMessageDialog(null,"E necessario digitar no campo");
        }

    gravar_no_arquivo();

    }//GEN-LAST:event_btn_salvarActionPerformed

    private void jTable1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTable1KeyTyped
        // TODO add your handling code here:
    }//GEN-LAST:event_jTable1KeyTyped

    private void jTable1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTable1KeyReleased
        // TODO add your handling code here:
        setField();
    }//GEN-LAST:event_jTable1KeyReleased

    private void tf_prd_valorKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tf_prd_valorKeyReleased
        // TODO add your handling code here:
        tf_prd_valor.setText(tf_prd_valor.getText().replaceAll("[^0-9 | ^.]", ""));
    }//GEN-LAST:event_tf_prd_valorKeyReleased

    private void jTable1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTable1KeyPressed
        // TODO add your handling code here:

    }//GEN-LAST:event_jTable1KeyPressed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // TODO add your handling code here:
        setField();
    }//GEN-LAST:event_jTable1MouseClicked

    private void tf_nome_prdKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tf_nome_prdKeyPressed
        // TODO add your handling code here:
        if(evt.getKeyCode() == 10){

        tf_prd_valor.requestFocus(true);
        }
    }//GEN-LAST:event_tf_nome_prdKeyPressed

    private void tf_prd_valorKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tf_prd_valorKeyPressed
        // TODO add your handling code here:
        if(evt.getKeyCode() == 10){

        tf_tempo.requestFocus(true);

        }

    }//GEN-LAST:event_tf_prd_valorKeyPressed

    private void tf_tempoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tf_tempoKeyPressed
        // TODO add your handling code here:
          if(evt.getKeyCode() == 10){

        btn_add.requestFocus(true);

        }
    }//GEN-LAST:event_tf_tempoKeyPressed

    private void btn_salvarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_btn_salvarKeyPressed
        // TODO add your handling code here:
        if(evt.getKeyCode() == 10){


        gravar_no_arquivo();


        }
    }//GEN-LAST:event_btn_salvarKeyPressed

    private void btn_addKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_btn_addKeyPressed
        // TODO add your handling code here:
        if(evt.getKeyCode() ==10){

                // TODO add your handling code here:
       DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel();

       dtm.addRow(new Object[]{tf_fornecedor.getText(), tf_nome_prd.getText(),tf_prd_valor.getText(),tf_tempo.getText()});

       prod_forn.add(new prd_fornecedor(tf_fornecedor.getText(), tf_nome_prd.getText(),tf_prd_valor.getText(),tf_tempo.getText()));

        }
        btn_salvar.requestFocus();
    }//GEN-LAST:event_btn_addKeyPressed

 public void gravar_no_arquivo(){



        try{

            ObjectOutputStream arquivo_prod_forn= new ObjectOutputStream(new FileOutputStream(new File ("docs/prod_forn.dat")));
            arquivo_prod_forn.writeObject(prod_forn);
        }catch(Exception erro){

        JOptionPane.showMessageDialog(null,"ERRO NA GRAVAÇÂO");

        }
        System.out.println("Gravado com Sucesso");

    }
private void carrega_arquivo(){

             DefaultTableModel tabela = (DefaultTableModel) jTable1.getModel();
             prd_fornecedor obj_prod_forn;

                try{

                    ObjectInputStream arquivo_prod_forn_ler = new ObjectInputStream(new FileInputStream(new File ("docs/prod_forn.dat")));
                    prod_forn = (ArrayList) arquivo_prod_forn_ler.readObject();


                }catch(ClassNotFoundException erro){
                            JOptionPane.showMessageDialog(null,"a classe nao foi encontrada "+erro);

                }
                 catch(FileNotFoundException erro)
                {
                  JOptionPane.showMessageDialog(null,"o arquivo nao foi encotrado"+erro);
                 }
                  catch(Exception erro){

                JOptionPane.showMessageDialog(null,"ERRO AO GRAVAR!!"+erro);
                }

               Object campos[] = new Object[4];

               Iterator iterar;

               iterar = prod_forn.iterator();

               while(iterar.hasNext()){

                   obj_prod_forn = (prd_fornecedor) iterar.next();

                   campos[0] = obj_prod_forn.getNome_fornecedor();
                   campos[1] = obj_prod_forn.getNome();
                   campos[2] = obj_prod_forn.getValor();
                   campos[3] = obj_prod_forn.getTempo_entrega();

                    tabela.addRow(campos);
               }


         if(jTable1.getRowCount() > 0){

             jTable1.setRowSelectionInterval(0, 0);
             setField();



        }



}

public void setField(){

  int linha_selecionada = jTable1.getSelectedRow();

      tf_fornecedor.setText(jTable1.getValueAt(linha_selecionada,0).toString());
      tf_nome_prd.setText(jTable1.getValueAt(linha_selecionada, 1).toString());
      tf_prd_valor.setText(jTable1.getValueAt(linha_selecionada, 2).toString());
      tf_tempo.setText( jTable1.getValueAt(linha_selecionada, 3).toString());


}
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(cad_prod_forn.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(cad_prod_forn.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(cad_prod_forn.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(cad_prod_forn.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new cad_prod_forn().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_add;
    private javax.swing.JButton btn_editar_prd;
    private javax.swing.JButton btn_excluir_prd;
    private javax.swing.JButton btn_novo_prd;
    private javax.swing.JButton btn_salvar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField tf_fornecedor;
    private javax.swing.JTextField tf_nome_prd;
    private javax.swing.JTextField tf_prd_valor;
    private javax.swing.JTextField tf_tempo;
    // End of variables declaration//GEN-END:variables
}
